//
//  WPPlaceAnOrderAlertView.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/4/18.
//  Copyright © 2016年 WPJoe. All rights reserved.
//

#import "WPPlaceAnOrderAlertView.h"
#define DURATION 0.5f
static NSString * detailTxt = @"您已成功下单，请保持电话畅通，项目方将尽快与您联系，协商具体取票事项。";

@interface WPPlaceAnOrderAlertView ()
@property (nonatomic, strong) UIView * redBackView;/**< 红色背景图 */
@property (nonatomic, strong) UIImageView * hookImageView;/**< 勾 */
@property (nonatomic, strong) UILabel * titleLabel;/**< 标题文本 */
@property (nonatomic, strong) UIButton * cancelbtn;/**< 取消按钮 */
@property (nonatomic , strong) UIView * mask;/**< 遮罩视图 */
@property (nonatomic, strong) UILabel * detailLabel;/**< 详细提示 */
@property (nonatomic, strong) UILabel * contactNameLabel;/**< 联系人姓名文本 */
@property (nonatomic, strong) NSString * contactName;/**< 联系人姓名 */
@property (nonatomic, strong) NSString * contactPhone;/**< 联系人电话 */
@property (nonatomic, strong) UILabel * contactPhoneLabel;/**< 联系人电话文本 */
@property (nonatomic, strong) UIButton * showOrderBtn;/**< 查看按钮 */
@property (nonatomic, copy) void (^complete)(NSString *result);/**< 结果block */
- (void)initializeAppearance;/**< 一般界面初始化 */
@end
@implementation WPPlaceAnOrderAlertView

+ (WPPlaceAnOrderAlertView *)showViewWithSize:(CGRect)rect andWithContactName:(NSString *)contactName andWithContactPhone:(NSString *)contactPhone WithComplete:(void (^)(NSString *))complete {
    WPPlaceAnOrderAlertView * view = [[WPPlaceAnOrderAlertView alloc]initWithFrame:rect withContactPhoe:contactPhone andWithContactName:contactName andWithComplete:complete];
    [view show];
    return view;
}
- (instancetype)initWithFrame:(CGRect)frame withContactPhoe:(NSString *)contactPhone andWithContactName:(NSString *)contactName andWithComplete:(void (^)(NSString *))complete {
    if ([super initWithFrame:frame]) {
        _contactName = contactName;
        _contactPhone = contactPhone;
        _complete = complete;
        [self initializeAppearance];
    }
    return self;
}
#pragma mark - init 
- (void)initializeAppearance {
    if (self.frame.size.height == 0) {
        self.bounds = CGRectMake(0, 0, SCREEN_SIZE.width*0.8, SCREEN_SIZE.height * 0.7);
        self.center = [UIApplication sharedApplication].keyWindow.center;
    }
    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 10;
    self.layer.masksToBounds = YES;
    [self addSubview:self.redBackView];
    [self addSubview:self.hookImageView];
    [self addSubview:self.titleLabel];
    [self addSubview:self.cancelbtn];
    [self addSubview:self.detailLabel];
//    UIView * line = [[UIView alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(_detailLabel.frame), self.frame.size.width-20, 0.5)];
//    line.backgroundColor = COLOR_RGB(221, 221, 221, 1);
//    [self addSubview:line];
//    [self addSubview:self.contactNameLabel];
//    [self addSubview:self.contactPhoneLabel];
    [self addSubview:self.showOrderBtn];
}
#pragma mark - responds event
- (void)respondsToCancelBtn {
    [self hide];
    _complete(@"back");
}
- (void)respondsToShowOrderBtn {
    [self hide];
    _complete(@"查看订单");
}
#pragma mark 显示\隐藏动画
- (void)show {
    [[UIApplication sharedApplication].keyWindow addSubview:self.mask];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.alpha = 0.0;
    [UIView animateWithDuration:DURATION animations:^{
        self.alpha = 1.0;
    } completion:nil];
}

- (void)hide {
    [UIView animateWithDuration:DURATION animations:^{
        self.mask.alpha = 0.0;
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [self.mask removeFromSuperview];
    }];
}
#pragma mark - getter
- (UIView *)mask {
    if (!_mask) {
        _mask = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, SCREEN_SIZE.height)];
            view.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.3];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
            [view addGestureRecognizer:tap];
            view;
        });
    }
    return _mask;
}
- (UIView *)redBackView {
    if (!_redBackView) {
        _redBackView = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 270*DHFlexibleHorizontalMutiplier(), 90*DHFlexibleHorizontalMutiplier())];
            view.backgroundColor = COLOR_RGB(234, 27, 47, 1);
            view;
        });
    }
    return _redBackView;
}
- (UIImageView *)hookImageView {
    if (!_hookImageView) {
        _hookImageView = ({
            UIImageView * view = [[UIImageView alloc]initWithFrame:CGRectMake(80*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier(), 20*DHFlexibleHorizontalMutiplier(), 20*DHFlexibleHorizontalMutiplier())];
            view.image = IMAGE_NAME(@"jycg");
            view;
        });
    }
    return _hookImageView;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = ({
            UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(120*DHFlexibleHorizontalMutiplier(), 25*DHFlexibleHorizontalMutiplier(), 200, 40*DHFlexibleHorizontalMutiplier())];
            label.font = [UIFont systemFontOfSize:16*DHFlexibleHorizontalMutiplier()];
            label.textColor = [UIColor whiteColor];
            label.text = @"下单成功";
            label;
        });
    }
    return _titleLabel;
}

- (UIButton *)cancelbtn {
    if (!_cancelbtn) {
        _cancelbtn = ({
            UIButton * btn =[ UIButton buttonWithType:UIButtonTypeCustom];
            btn.bounds = CGRectMake(0, 0, 22*DHFlexibleHorizontalMutiplier(), 22*DHFlexibleHorizontalMutiplier());
            btn.center = CGPointMake(CGRectGetMaxX(self.bounds)-21*DHFlexibleHorizontalMutiplier(), 20*DHFlexibleHorizontalMutiplier());
            [btn setImage:[UIImage imageNamed:@"alertViewCancel"] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(respondsToCancelBtn) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _cancelbtn;
}
- (UILabel *)detailLabel {
    if (!_detailLabel) {
        _detailLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(self.redBackView.frame), CGRectGetWidth(self.bounds)-40, 90*DHFlexibleHorizontalMutiplier())];
            lab.numberOfLines = 0;
            lab.font = [UIFont systemFontOfSize:13*DHFlexibleHorizontalMutiplier()];
            lab.text = detailTxt;
            lab;
        });
    }
    return _detailLabel;
}
- (UILabel *)contactNameLabel {
    if (!_contactNameLabel) {
        _contactNameLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.detailLabel.frame), CGRectGetMaxY(self.detailLabel.frame)+10*DHFlexibleHorizontalMutiplier(), 250, 25*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            lab.text = [NSString stringWithFormat:@"项目联系人：%@", _contactName];
            lab;
        });
    }
    return _contactNameLabel;
}
- (UILabel *)contactPhoneLabel {
    if (!_contactPhoneLabel) {
        _contactPhoneLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.detailLabel.frame), CGRectGetMaxY(self.contactNameLabel.frame), 250, CGRectGetHeight(self.contactNameLabel.bounds))];
            lab.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            lab.text = [NSString stringWithFormat:@"联系电话：%@", _contactPhone];
            lab;
        });
    }
    return _contactPhoneLabel;
}
- (UIButton *)showOrderBtn {
    if (!_showOrderBtn) {
        _showOrderBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(50*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.bounds) - 40*DHFlexibleHorizontalMutiplier(), 170*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier());
            btn.backgroundColor = COLOR_RGB(234, 27, 47, 1);
            btn.layer.cornerRadius = 5*DHFlexibleHorizontalMutiplier();
            [btn setTitle:@"查看我的订单" forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            [btn addTarget:self action:@selector(respondsToShowOrderBtn) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _showOrderBtn;
}
@end
